package gr.handmademarket.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * hibernate utility. 
 */
public class HibernateUtil {

	/**
	 * session factory.
	 */
	private static final SessionFactory sessionFactory = buildSessionFactory();

    /**
     * builds session factory.
     * @return session factory.
     */
    @SuppressWarnings("nls")
	private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
        	//return (SessionFactory) new InitialContext().lookup("java:comp/env/jdbc/postgres");
            return new Configuration().configure("/gr/handmademarket/resources/hibernate.cfg.xml").buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    /**
     * @return session factory.
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}
